Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@noble/ed25519
Advanced tools
Fastest 4KB JS implementation of ed25519 EDDSA signatures compliant with RFC8032, FIPS 186-5 & ZIP215
@noble/ed25519 is a JavaScript library for the Ed25519 public-key signature system. It provides functionalities for key generation, signing, and verification using the Ed25519 algorithm, which is known for its high performance and security.
Key Generation
This feature allows you to generate a new pair of public and private keys using the Ed25519 algorithm.
const { generateKeyPair } = require('@noble/ed25519');
(async () => {
const { publicKey, privateKey } = await generateKeyPair();
console.log('Public Key:', publicKey);
console.log('Private Key:', privateKey);
})();
Signing
This feature allows you to sign a message using a private key. The resulting signature can be used to verify the authenticity of the message.
const { sign, generateKeyPair } = require('@noble/ed25519');
(async () => {
const { privateKey } = await generateKeyPair();
const message = new TextEncoder().encode('Hello, world!');
const signature = await sign(message, privateKey);
console.log('Signature:', signature);
})();
Verification
This feature allows you to verify a signature using the corresponding public key. It ensures that the message was signed by the holder of the private key.
const { verify, generateKeyPair, sign } = require('@noble/ed25519');
(async () => {
const { publicKey, privateKey } = await generateKeyPair();
const message = new TextEncoder().encode('Hello, world!');
const signature = await sign(message, privateKey);
const isValid = await verify(signature, message, publicKey);
console.log('Is the signature valid?', isValid);
})();
TweetNaCl is a cryptographic library that provides similar functionalities for the Ed25519 algorithm, including key generation, signing, and verification. It is known for its simplicity and small size, making it suitable for environments with limited resources.
Libsodium is a widely-used cryptographic library that offers a comprehensive set of cryptographic primitives, including support for the Ed25519 algorithm. It is known for its high performance and security, and it provides additional features such as encryption and key exchange.
Elliptic is a JavaScript library for elliptic curve cryptography, including support for various algorithms such as Ed25519. It provides a flexible and modular approach to cryptographic operations, making it suitable for a wide range of applications.
Fastest 4KB JS implementation of ed25519 signatures.
Use larger drop-in replacement noble-curves instead, if you need additional features such as common.js support, ristretto255, X25519, curve25519, ed25519ph, ed25519ctx. To upgrade from v1 to v2, see Upgrading. Online demo.
noble-cryptography — high-security, easily auditable set of contained cryptographic libraries and tools.
npm install @noble/ed25519
We support all major platforms and runtimes. For node.js <= 18 and React Native, additional polyfills are needed: see below.
import * as ed from '@noble/ed25519';
// import * as ed from "https://deno.land/x/ed25519/mod.ts"; // Deno
// import * as ed from "https://unpkg.com/@noble/ed25519"; // Unpkg
(async () => {
// keys, messages & other inputs can be Uint8Arrays or hex strings
// Uint8Array.from([0xde, 0xad, 0xbe, 0xef]) === 'deadbeef'
const privKey = ed.utils.randomPrivateKey(); // Secure random private key
const message = Uint8Array.from([0xab, 0xbc, 0xcd, 0xde]);
const pubKey = await ed.getPublicKeyAsync(privKey); // Sync methods below
const signature = await ed.signAsync(message, privKey);
const isValid = await ed.verifyAsync(signature, message, pubKey);
})();
Additional polyfills for some environments:
// 1. Enable synchronous methods.
// Only async methods are available by default, to keep the library dependency-free.
import { sha512 } from '@noble/hashes/sha512';
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
// Sync methods can be used now:
// ed.getPublicKey(privKey); ed.sign(msg, privKey); ed.verify(signature, msg, pubKey);
// 2. node.js 18 and older, requires polyfilling globalThis.crypto
import { webcrypto } from 'node:crypto';
// @ts-ignore
if (!globalThis.crypto) globalThis.crypto = webcrypto;
// 3. React Native needs crypto.getRandomValues polyfill and sha512
import 'react-native-get-random-values';
import { sha512 } from '@noble/hashes/sha512';
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
ed.etc.sha512Async = (...m) => Promise.resolve(ed.etc.sha512Sync(...m));
There are 3 main methods: getPublicKey(privateKey)
, sign(message, privateKey)
and verify(signature, message, publicKey)
. We accept Hex type everywhere:
type Hex = Uint8Array | string
function getPublicKey(privateKey: Hex): Uint8Array;
function getPublicKeyAsync(privateKey: Hex): Promise<Uint8Array>;
Generates 32-byte public key from 32-byte private key.
priv64b.slice(0, 32)
Point.fromPrivateKey(privateKey)
if you want Point
instance insteadPoint.fromHex(publicKey)
if you want to convert hex / bytes into Point.
It will use decompression algorithm 5.1.3 of RFC 8032.utils.getExtendedPublicKey
if you need full SHA512 hash of seedfunction sign(
message: Hex, // message which would be signed
privateKey: Hex // 32-byte private key
): Uint8Array;
function signAsync(message: Hex, privateKey: Hex): Promise<Uint8Array>;
Generates EdDSA signature. Always deterministic.
Assumes unhashed message
: it would be hashed by ed25519 internally.
For prehashed ed25519ph, switch to noble-curves.
function verify(
signature: Hex, // returned by the `sign` function
message: Hex, // message that needs to be verified
publicKey: Hex // public (not private) key,
options = { zip215: true } // ZIP215 or RFC8032 verification type
): boolean;
function verifyAsync(signature: Hex, message: Hex, publicKey: Hex): Promise<boolean>;
Verifies EdDSA signature. Has SUF-CMA (strong unforgeability under chosen message attacks).
By default, follows ZIP215 1 and can be used in consensus-critical apps 2.
zip215: false
option switches verification criteria to strict
RFC8032 / FIPS 186-5 and provides non-repudiation with SBS (Strongly Binding Signatures) 3.
A bunch of useful utilities are also exposed:
const etc: {
bytesToHex: (b: Bytes) => string;
hexToBytes: (hex: string) => Bytes;
concatBytes: (...arrs: Bytes[]) => Uint8Array;
mod: (a: bigint, b?: bigint) => bigint;
invert: (num: bigint, md?: bigint) => bigint;
randomBytes: (len: number) => Bytes;
sha512Async: (...messages: Bytes[]) => Promise<Bytes>;
sha512Sync: Sha512FnSync;
};
const utils: {
getExtendedPublicKeyAsync: (priv: Hex) => Promise<ExtK>;
getExtendedPublicKey: (priv: Hex) => ExtK;
precompute(p: Point, w?: number): Point;
randomPrivateKey: () => Bytes; // Uses CSPRNG https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
};
class ExtendedPoint { // Elliptic curve point in Extended (x, y, z, t) coordinates.
constructor(ex: bigint, ey: bigint, ez: bigint, et: bigint);
static readonly BASE: Point;
static readonly ZERO: Point;
static fromAffine(point: AffinePoint): ExtendedPoint;
static fromHex(hash: string);
get x(): bigint;
get y(): bigint;
// Note: It does not check whether the `other` point is valid point on curve.
add(other: ExtendedPoint): ExtendedPoint;
equals(other: ExtendedPoint): boolean;
isTorsionFree(): boolean; // Multiplies the point by curve order
multiply(scalar: bigint): ExtendedPoint;
subtract(other: ExtendedPoint): ExtendedPoint;
toAffine(): Point;
toRawBytes(): Uint8Array;
toHex(): string; // Compact representation of a Point
}
// Curve params
ed25519.CURVE.p // 2 ** 255 - 19
ed25519.CURVE.n // 2 ** 252 + 27742317777372353535851937790883648493
ed25519.ExtendedPoint.BASE // new ed25519.Point(Gx, Gy) where
// Gx=15112221349535400772501151409588531511454012693041857206046113283949847762202n
// Gy=46316835694926478169428394003475163141307993866256225615783033603165251855960n;
The library has not been independently audited as of v2, which is a rewrite of v1. v1 has been audited by Cure53 in Feb 2022.
The code is identical to noble-curves, which has been audited.
It is tested against property-based, cross-library and Wycheproof vectors, and has fuzzing by Guido Vranken's cryptofuzz.
JIT-compiler and Garbage Collector make "constant time" extremely hard to achieve timing attack resistance in a scripting language. Which means any other JS library can't have constant-timeness. Even statically typed Rust, a language without GC, makes it harder to achieve constant-time for some cases. If your goal is absolute security, don't use any JS lib — including bindings to native ones. Use low-level libraries & languages. Nonetheless we're targetting algorithmic constant time.
npm-diff
We consider infrastructure attacks like rogue NPM modules very important; that's why it's crucial to minimize the amount of 3rd-party dependencies & native bindings. If your app uses 500 dependencies, any dep could get hacked and you'll be downloading malware with every install. Our goal is to minimize this attack vector.
If you see anything unusual: investigate and report.
We're deferring to built-in crypto.getRandomValues which is considered cryptographically secure (CSPRNG).
In the past, browsers had bugs that made it weak: it may happen again.
Benchmarks done with Apple M2 on macOS 13 with Node.js 20.
getPublicKey(utils.randomPrivateKey()) x 9,173 ops/sec @ 109μs/op
sign x 4,567 ops/sec @ 218μs/op
verify x 994 ops/sec @ 1ms/op
Point.fromHex decompression x 16,164 ops/sec @ 61μs/op
Compare to alternative implementations:
tweetnacl@1.0.3 getPublicKey x 1,808 ops/sec @ 552μs/op ± 1.64%
tweetnacl@1.0.3 sign x 651 ops/sec @ 1ms/op
ristretto255@0.1.2 getPublicKey x 640 ops/sec @ 1ms/op ± 1.59%
sodium-native#sign x 83,654 ops/sec @ 11μs/op
npm install
to install build dependencies like TypeScriptnpm run build
to compile TypeScript codenpm run test
to run testsnoble-ed25519 v2 features improved security and smaller attack surface. The goal of v2 is to provide minimum possible JS library which is safe and fast.
That means the library was reduced 4x, to just over 300 lines. In order to achieve the goal, some features were moved to noble-curves, which is even safer and faster drop-in replacement library with same API. Switch to curves if you intend to keep using these features:
utils.precompute()
for non-base pointOther changes for upgrading from @noble/ed25519 1.7 to 2.0:
getPublicKeyAsync
, signAsync
, verifyAsync
for async versionsbigint
is no longer allowed in getPublicKey
, sign
, verify
. Reason: ed25519 is LE, can lead to bugsPoint
(2d xy) has been changed to ExtendedPoint
(xyzt)Signature
was removed: just use raw bytes or hex nowutils
were split into utils
(same api as in noble-curves) and
etc
(sha512Sync
and others)MIT (c) 2019 Paul Miller (https://paulmillr.com), see LICENSE file.
FAQs
Fastest 4KB JS implementation of ed25519 EDDSA signatures compliant with RFC8032, FIPS 186-5 & ZIP215
The npm package @noble/ed25519 receives a total of 90,787 weekly downloads. As such, @noble/ed25519 popularity was classified as popular.
We found that @noble/ed25519 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.